package org.example.panel.datapanel;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import org.jfree.chart.*;
import org.example.operation.*;
import org.jfree.data.category.*;

/**
 * @author lenovo
 */

public class GraphDataPanel extends JPanel {
    public GraphDataPanel(){
        //组件准备
        JTextField courseName = new JTextField(15);
        JButton graphDataBar = new JButton("导出柱形图");
        JButton graphDataBarFrame = new JButton("查看柱形图");
        ChartPanel chartPanel = new ChartPanel(ChartFactory.createBarChart(null,
                null,null,new DefaultCategoryDataset()));
        chartPanel.setPreferredSize(new Dimension(440,250));
        JScrollPane scrollPane = new JScrollPane(chartPanel);
        scrollPane.setPreferredSize(new Dimension(450,250));
        Box coursebox = Box.createHorizontalBox();
        Box box = Box.createVerticalBox();
        //注册监视器
        graphDataBar.addActionListener((ActionEvent e)->{
            if(courseName.getText().matches(".*\\s+.*")){
                JOptionPane.showMessageDialog(null,
                        "课程名不能有空格", null, JOptionPane.WARNING_MESSAGE);
            }else if(!courseName.getText().isEmpty() &&
                    !CourseOperation.isCourse(courseName.getText())){
                JOptionPane.showMessageDialog(null,
                        "课程管理中无此课程", null, JOptionPane.WARNING_MESSAGE);
            }else{
                JFileChooser fileChooser = new JFileChooser();
                if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
                    File file = fileChooser.getSelectedFile();
                    String[] paths = file.getAbsolutePath().split("[.]");
                    if (!"png".equals(paths[paths.length - 1]) &&
                            !"PNG".equals(paths[paths.length - 1])) {
                        JOptionPane.showMessageDialog(null,
                                "文件必须为.png文件", null, JOptionPane.WARNING_MESSAGE);
                    }else{
                        try{
                            ChartUtils.saveChartAsPNG(file,GraphOperation.graphSegmentScore
                                    (courseName.getText(),25),2000,800);
                            JOptionPane.showMessageDialog(null,
                                    "导出成功", null, JOptionPane.WARNING_MESSAGE);
                        }catch(Exception e2) {
                            //
                        }
                    }
                }
            }
        });
        graphDataBarFrame.addActionListener((ActionEvent e)->{
            if(courseName.getText().matches(".*\\s+.*")){
                JOptionPane.showMessageDialog(null,
                        "课程名不能有空格", null, JOptionPane.WARNING_MESSAGE);
            }else if(!courseName.getText().isEmpty() &&
                    !CourseOperation.isCourse(courseName.getText())){
                JOptionPane.showMessageDialog(null,
                        "课程管理中无此课程", null, JOptionPane.WARNING_MESSAGE);
            }else{
                chartPanel.setChart(GraphOperation.graphSegmentScore(
                        courseName.getText(), 10));
                validate();
            }
        });
        //添加组件
        coursebox.add(new JLabel("课程名： "));
        coursebox.add(courseName);
        coursebox.add(Box.createHorizontalStrut(5));
        coursebox.add(graphDataBar);
        coursebox.add(Box.createHorizontalStrut(5));
        coursebox.add(graphDataBarFrame);
        box.add(coursebox);
        box.add(Box.createVerticalStrut(5));
        box.add(scrollPane);
        add(box);
    }
}
